<?php 
class InterviewRecordModel extends Model{
	/**
	 * @author yinyi
	 * @description  大家的排行榜
	 * @date 2010-06-11
	 */
	function allRecord(){
		return $this->query("SELECT 
								a.uid,
								b.`scores`, 
								c.name,
							    count(*) as count
							FROM 
								ts_interview_record as a,`ts_interview_scores` as b,ts_user as c 
							WHERE
								a.uid = b.uid and a.uid = c.id 
							GROUP BY 
								a.uid
							ORDER BY 
								a.answer_time 
							LIMIT 100");
								}

	/**
	 * @author yinyi
	 * @description 好友的排行榜
	 * @param $friends 好友的ID集合
	 * @date 2010-06-11
	 */
	function friendRecord($friends){
		return $this->query("SELECT 
							    a.uid,
							    b.`scores`, 
							    c.name,
							    count(*) as count
							FROM 
							    ts_interview_record as a,`ts_interview_scores` as b,ts_user as c 
							WHERE
							    a.uid = b.uid and a.uid = c.id and c.id in (".$friends.")
							GROUP BY 
							    a.uid
							ORDER BY 
							    a.answer_time 
							LIMIT 100");
	}

	/**
	 * @author yinyi
	 * @description 所有做错的题目的Id
	 * @date 2010-06-11
	 */
	function wrongRecord($uid){
		$interviewRecords = $this->query("SELECT *
							FROM
							  ts_interview_record
							WHERE
							  uid = ".$uid." AND 
							  progress_flag = 1 AND 
							  jude = 0");
		
		$pids = array();
		foreach($interviewRecords as $interviewRecord){
			$pids[] = $interviewRecord['pid'];
		}
		return $pids;
		
	}
	
	/**
	 * @author yinyi
	 * @description 做对的好友
	 * @date 2010-06-11
	 */
	function doRightFriends($pid,$friends){
		return $this->query("SELECT a.*,b.name
							FROM
							  ts_interview_record  as a,ts_user as b
							WHERE
							  a.jude = 1 AND 
							  a.pid = ".$pid." AND 
							  a.uid = b.id and
							  a.uid IN (".$friends.")
							ORDER BY
							  a.answer_time
							LIMIT 10");
	}
	
	/**
	 * @author yinyi
	 * @description 做错的好友
	 * @date 2010-06-11
	 */
	function doWrongFriends($pid,$friends){
		return $this->query("SELECT a.*,b.name
							FROM
							  ts_interview_record  as a,ts_user as b
							WHERE
							  a.jude = 0 AND 
							  a.pid = ".$pid." AND 
							  a.uid = b.id and
							  a.uid IN (".$friends.")
							ORDER BY
							  a.answer_time 
							LIMIT 10");
	}
	
	/**
	 * @author yinyi
	 * @description 做对或做错的所有人
	 * @date 2010-06-12
	 */
	function doWrongAll($uid,$pid,$friends,$flag){
		if($flag){
			return $this->query("SELECT a.*,b.name
								FROM
								  ts_interview_record  as a,ts_user as b
								WHERE
								  a.jude = 1 AND 
								  a.pid = ".$pid." AND 
								  a.uid = b.id AND
								  a.uid NOT IN (".$friends.") AND
								  a.uid <> $uid
								 ORDER BY
								  a.answer_time");
		}else{
			return $this->query("SELECT a.*,b.name
								FROM
								  ts_interview_record  as a,ts_user as b
								WHERE
								  a.jude = 0 AND 
								  a.pid = ".$pid." AND 
								  a.uid = b.id AND
								  a.uid NOT IN (".$friends.") AND
								  a.uid <> $uid
								ORDER BY
								  a.answer_time");
		}
	}
}
?>